﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Data;
using System.Windows.Forms;
using System.Globalization;


namespace HCommon
{
    public class Common
    {
        public static string AppDir = Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location);
        public static string LogPath = Path.Combine(AppDir, "ErrLog.txt");
        public static string ActionPath = Path.Combine(AppDir, "ActionLog.txt");
        private static string BreakLine = "==================================================";


        public static void GhiActionLog(string Title, string MessContent)
        {
            try
            {
                if (!File.Exists(ActionPath))
                {
                    File.WriteAllText(ActionPath, Environment.NewLine + BreakLine + Environment.NewLine + "Title : " + Title + Environment.NewLine + "Date : " + DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss") + Environment.NewLine + "Action Log : " + MessContent);
                }
                else
                {
                    File.AppendAllText(ActionPath, Environment.NewLine + BreakLine + Environment.NewLine + "Title : " + Title + Environment.NewLine + "Date : " + DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss") + Environment.NewLine + "Action Log : " + MessContent);
                }
            }
            catch { }
        }

        public static void GhiLog(string Title, string ErrContent)
        {
            try
            {
                if (!File.Exists(LogPath))
                {
                    File.WriteAllText(LogPath, Environment.NewLine + BreakLine + Environment.NewLine + "Title : " + Title + Environment.NewLine + "Date : " + DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss") + Environment.NewLine + "Message : " + ErrContent);
                }
                else
                {
                    File.AppendAllText(LogPath, Environment.NewLine + BreakLine + Environment.NewLine + "Title : " + Title + Environment.NewLine + "Date : " + DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss") + Environment.NewLine + "Message : " + ErrContent);
                }
            }
            catch { }
        }

        public static void GhiLog(string Title, Exception ex)
        {
            try
            {
                if (!File.Exists(LogPath))
                {
                    if (ex.InnerException != null)
                    {
                        File.WriteAllText(LogPath, Environment.NewLine + BreakLine + Environment.NewLine + "Title : " + Title + Environment.NewLine + "Date : " + DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss") + Environment.NewLine + "Message : " + ex.Message + Environment.NewLine + "Inner : " + ex.InnerException + Environment.NewLine + "StackTrace : " + ex.StackTrace);
                    }
                    else
                    {
                        File.WriteAllText(LogPath, Environment.NewLine + BreakLine + Environment.NewLine + "Title : " + Title + Environment.NewLine + "Date : " + DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss") + Environment.NewLine + "Message : " + ex.Message + Environment.NewLine + "StackTrace : " + ex.StackTrace);
                    }
                }
                else
                {
                    if (ex.InnerException != null)
                    {
                        File.AppendAllText(LogPath, Environment.NewLine + BreakLine + Environment.NewLine + "Title : " + Title + Environment.NewLine + "Date : " + DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss") + Environment.NewLine + "Message : " + ex.Message + Environment.NewLine + "Inner : " + ex.InnerException + Environment.NewLine + "StackTrace : " + ex.StackTrace);
                    }
                    else
                    {
                        File.AppendAllText(LogPath, Environment.NewLine + BreakLine + Environment.NewLine + "Title : " + Title + Environment.NewLine + "Date : " + DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss") + Environment.NewLine + "Message : " + ex.Message + Environment.NewLine + "StackTrace : " + ex.StackTrace);
                    }
                }
            }
            catch { }
        }

        public static void GhiLog(string Title, string PreMess, Exception ex)
        {
            try
            {
                if (!File.Exists(LogPath))
                {
                    if (ex.InnerException != null)
                    {
                        File.WriteAllText(LogPath, Environment.NewLine + BreakLine + Environment.NewLine + "Title : " + Title + Environment.NewLine + "Date : " + DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss") + Environment.NewLine + "More Info : " + PreMess + Environment.NewLine + "Message : " + ex.Message + Environment.NewLine + "Inner : " + ex.InnerException + Environment.NewLine + "StackTrace : " + ex.StackTrace);
                    }
                    else
                    {
                        File.WriteAllText(LogPath, Environment.NewLine + BreakLine + Environment.NewLine + "Title : " + Title + Environment.NewLine + "Date : " + DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss") + Environment.NewLine + "More Info : " + PreMess + Environment.NewLine + "Message : " + ex.Message + Environment.NewLine + "StackTrace : " + ex.StackTrace);
                    }
                }
                else
                {
                    if (ex.InnerException != null)
                    {
                        File.AppendAllText(LogPath, Environment.NewLine + BreakLine + Environment.NewLine + "Title : " + Title + Environment.NewLine + "Date : " + DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss") + Environment.NewLine + "More Info : " + PreMess + Environment.NewLine + "Message : " + ex.Message + Environment.NewLine + "Inner : " + ex.InnerException + Environment.NewLine + "StackTrace : " + ex.StackTrace);
                    }
                    else
                    {
                        File.AppendAllText(LogPath, Environment.NewLine + BreakLine + Environment.NewLine + "Title : " + Title + Environment.NewLine + "Date : " + DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss") + Environment.NewLine + "More Info : " + PreMess + Environment.NewLine + "Message : " + ex.Message + Environment.NewLine + "StackTrace : " + ex.StackTrace);
                    }
                }
            }
            catch { }
        }

        public static string xoaNhay(string InpStr)
        {
            try
            {
                return InpStr.Replace("'", "''");
            }
            catch (Exception)
            {
                return InpStr;
            }
        }

        public static bool checkStr(string InpStr)
        {
            if (InpStr != null && InpStr.Trim().Length > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public static bool checkDt(DataTable dt)
        {
            if (dt != null && dt.Rows.Count > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public static bool isNum(string numStr)
        {
            try
            {
                int num;
                return int.TryParse(numStr.Trim(), out num);
            }
            catch (Exception ex)
            {
                return false;
            }

        }

        public static string cutPath(int left, int right, string filePath)
        {

            string filePathCut = "";
            try
            {
                string[] fileFullPathCut = filePath.Split('\\');
                if (fileFullPathCut.Length > 0)
                {
                    for (int i = left; i < fileFullPathCut.Length - right; i++)
                    {
                        filePathCut = Path.Combine(filePathCut, fileFullPathCut[i]);
                    }
                }
            }
            catch (Exception ex)
            {
                GhiLog("cutPath", ex);
            }
            return filePathCut;
        }

        public static void LoadCombo(ComboBox cbo, string MajorCd, bool isAllowNull)
        {
            try
            {
                cbo.DisplayMember = "MinorName";
                cbo.ValueMember = "MinorCd";
                DataTable dt = new DataTable();
                if (isAllowNull)
                {
                    dt = Dao.Get_Table("SELECT '' AS MinorCd,N'' AS MinorName UNION ALL SELECT MinorCd,MinorName FROM tblMinor WITH(NOLOCK) WHERE MinorCd like '" + MajorCd + "%'");
                }
                else
                {
                    dt = Dao.Get_Table("SELECT MinorCd,MinorName FROM tblMinor WITH(NOLOCK) WHERE MinorCd like '" + MajorCd + "%'");
                }
                cbo.DataSource = dt;
            }
            catch (Exception ex)
            {
                Common.GhiLog("LoadCombo", ex);
                Dialog.ShowMess(Dialog.mType.err, ex.Message);
            }
        }

        public static void LoadCombo(DataGridViewComboBoxColumn cbo, string MajorCd, bool isAllowNull)
        {
            try
            {
                cbo.DisplayMember = "MinorName";
                cbo.ValueMember = "MinorCd";
                DataTable dt = new DataTable();
                if (isAllowNull)
                {
                    dt = Dao.Get_Table("SELECT '' AS MinorCd,N'' AS MinorName UNION ALL SELECT MinorCd,MinorName FROM tblMinor WITH(NOLOCK) WHERE MinorCd like '" + MajorCd + "%'");
                }
                else
                {
                    dt = Dao.Get_Table("SELECT MinorCd,MinorName FROM tblMinor WITH(NOLOCK) WHERE MinorCd like '" + MajorCd + "%'");
                }
                cbo.DataSource = dt;
            }
            catch (Exception ex)
            {
                Common.GhiLog("LoadCombo", ex);
                Dialog.ShowMess(Dialog.mType.err, ex.Message);
            }
        }

        public static void ClearControl(GroupBox grb)
        {
            try
            {
                foreach (Control ctr in grb.Controls)
                {
                    if (ctr.GetType().Name.Equals("hTextBox"))
                    {
                        TextBox hText = (TextBox)ctr;
                        hText.Text = string.Empty;
                    }
                    else if (ctr.GetType().Name.Equals("hRichTextBox"))
                    {
                        RichTextBox hText = (RichTextBox)ctr;
                        hText.Text = string.Empty;
                    }
                }
            }
            catch (Exception ex) { throw ex; }
        }

        public static void ClearControl(Form frm)
        {
            try
            {
                foreach (Control ctr in frm.Controls)
                {
                    if (ctr.GetType().Name.Equals("hTextBox"))
                    {
                        TextBox hText = (TextBox)ctr;
                        hText.Text = string.Empty;
                    }
                    else if (ctr.GetType().Name.Equals("hRichTextBox"))
                    {
                        RichTextBox hText = (RichTextBox)ctr;
                        hText.Text = string.Empty;
                    }
                }
            }
            catch { }
        }

        public static string VNDateToSQL(string VNDate) 
        { 
            //VNDate có định dang dd/MM/yyyy
            try
            {
                DateTime dateVN = DateTime.ParseExact(VNDate, "dd/MM/yyyy", CultureInfo.InvariantCulture);
                return dateVN.ToString("yyyyMMdd");
            }
            catch { return ""; }

        }
    }
}
